package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.Ad;
import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.service.AdService;
import com.cskaoyan.utils.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Description
 * 推广模块的广告功能
 *
 * @author Zhangfei
 * @date Created on 2021/10/18
 */

@RestController
@RequestMapping("/admin/ad")
public class AdController {

    @Autowired
    AdService adService;

    /**
     * 通过分页显示广告接口
     *
     * @param param
     * @return
     */
    @RequiresPermissions("admin:ad:list")
    @GetMapping("/list")
    public BaseRespVo list(String name, String content, BaseParam param) {
        BasePageData data = adService.list(name, content, param);
        return BaseRespVo.ok(data);
    }

    /**
     * 添加广告方法
     *
     * @param ad
     * @return
     */
    @RequiresPermissions("admin:ad:create")
    @PostMapping("/create")
    public BaseRespVo create(@RequestBody Ad ad) {
        Ad adToReturn = adService.create(ad);
        if(CommonUtil.isNull(adToReturn)){
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(adToReturn);
    }


    /**
     * 修改广告
     *
     * @param ad
     * @return
     */
    @RequiresPermissions("admin:ad:update")
    @PostMapping("/update")
    public BaseRespVo update(@RequestBody Ad ad) {
        Ad adToReturn = adService.update(ad);
        return BaseRespVo.ok(adToReturn);
    }


    /**
     * 删除广告
     *
     * @param ad
     * @return
     */
    @RequiresPermissions("admin:ad:delete")
    @PostMapping("/delete")
    public BaseRespVo delete(@RequestBody Ad ad) {
        //执行删除，根据返回值响应相应的数据
        Integer count = adService.delete(ad);
        if (count != 0) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail("删除失败");
    }


}
